package com.javasm.service.goods.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.goods.ActivityCriteria;
import com.javasm.domin.entity.Admin;
import com.javasm.domin.entity.goods.Activity;
import com.javasm.domin.entity.goods.GoodsAdjust;
import com.javasm.domin.vo.goods.ActivityVo;
import com.javasm.mapper.goods.ActivityMapper;
import com.javasm.service.base.impl.BaseServiceimpl;
import com.javasm.service.goods.IActivityService;
import com.javasm.transfer.goods.ActivityTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whl
 * @since 2022-01-16
 */
@Service
@Transactional
@RequiredArgsConstructor
public class ActivityServiceImpl extends BaseServiceimpl< Activity> implements IActivityService {

    private final ActivityMapper activityMapper;
    private final ActivityTransfer activityTransfer;

    @Override
    public PageResult<ActivityVo> searchPage(ActivityCriteria activityCriteria) {
        PageHelper.startPage(activityCriteria.getCurrentPage(), activityCriteria.getPageSize());
        LambdaQueryWrapper<Activity> lambda = new QueryWrapper<Activity>().lambda();

        if (!StringUtils.isEmpty(activityCriteria.getGoodsName())) {
            lambda.like(Activity::getGoodsName, activityCriteria.getGoodsName());
        }
        if (!StringUtils.isEmpty(activityCriteria.getApproveStatus())) {
            lambda.eq(Activity::getApproveStatus, activityCriteria.getApproveStatus());
        }
        if (!StringUtils.isEmpty(activityCriteria.getStartTime())) {
            lambda.between(Activity::getEffectTime, activityCriteria.getStartTime(), activityCriteria.getEndTime());
        }
        List<Activity> activities = activityMapper.selectList(lambda);
        PageInfo<Activity> pageInfo = new PageInfo<>(activities);
        List<ActivityVo> activityVos = activityTransfer.setInfo(activities);
        return new PageResult<ActivityVo>(pageInfo.getTotal(), activityVos);
    }

    @Override
    public PageResult<ActivityVo> searchPage1(ActivityCriteria activityCriteria) {
        PageHelper.startPage(activityCriteria.getCurrentPage(), activityCriteria.getPageSize());
        LambdaQueryWrapper<Activity> lambda = new QueryWrapper<Activity>().lambda();

        if (!StringUtils.isEmpty(activityCriteria.getGoodsName())) {
            lambda.like(Activity::getGoodsName, activityCriteria.getGoodsName());
        }

        lambda.eq(Activity::getApproveStatus,0);
        if (!StringUtils.isEmpty(activityCriteria.getStartTime())) {
            lambda.between(Activity::getEffectTime, activityCriteria.getStartTime(), activityCriteria.getEndTime());
        }
        List<Activity> activities = activityMapper.selectList(lambda);
        PageInfo<Activity> pageInfo = new PageInfo<>(activities);
        List<ActivityVo> activityVos = activityTransfer.setInfo(activities);
        return new PageResult<ActivityVo>(pageInfo.getTotal(), activityVos);
    }
}
